﻿
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using RATSharp.Model;
using RATSharp.Rest;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Windows.Data;
using RATSharpW7.Converters;
namespace RATSharpW7.ViewModel.CalendarViewModel
{
    /// <summary>
    /// VieModel for user calendar
    /// </summary>
    [DataContract]
    public class DayViewUserModel : AbstractDayViewModel
    {
        public DayViewUserModel(DateTime selDate)
            : base(selDate)
        {
        }

        protected override Task<Reservations> getTask(DateTime firstDayInMonth, DateTime LastDayInMonth)
        {
            return RestService.getInstance().getReservationsByUserAsync(firstDayInMonth, LastDayInMonth);
        }

        protected override IDayViewColorProvider getColorProvider()
        {
            return new DayViewUserColorProvider();
        }

        public override bool isPeriodEditable(period period)
        {
            return period.FromDate.CompareTo(DateTime.Now) > 0 && period.reservation.userId == UserViewModel.UserContext.Id;
        }
    }

    /// <summary>
    /// Color provider specific for user calendar
    /// </summary>
    class DayViewUserColorProvider : IDayViewColorProvider
    {
        public SolidColorBrush provideColor(period period)
        {
            SolidColorBrush color;

            if (!period.Valid)
            {
                color = new SolidColorBrush(Colors.Gray);
            }
            else
            {

                if (period.FromDate.CompareTo(DateTime.Now) < 0)
                {
                    if (period.ToDate.CompareTo(DateTime.Now) < 0)
                    {
                        // past
                        if (period.returnAction == ReturnAction.NOT_RETURNED)
                        {
                            color = new SolidColorBrush(Colors.Red);
                        }
                        else
                        {
                            color = new SolidColorBrush(Colors.Gray);
                        }
                    }
                    else
                    {
                        // now
                        if (period.pickAction == PickAction.NOT_PICKED)
                        {
                            color = new SolidColorBrush(Colors.Red);
                        }
                        else
                        {
                            if (period.returnAction == ReturnAction.RETURNED)
                            {
                                color = new SolidColorBrush(Colors.Gray);
                            }
                            else if (period.returnAction == ReturnAction.NOT_RETURNABLE)
                            {
                                color = new SolidColorBrush(Colors.Yellow);
                            }
                            else
                            {
                                color = new SolidColorBrush(Colors.Green);
                            }

                        }
                    }

                }
                else
                {
                    // future
                    if (period.pickAction == PickAction.NOT_PICKED)
                    {
                        color = new SolidColorBrush(Colors.Blue);
                    }
                    else
                    {
                        color = new SolidColorBrush(Colors.Yellow);
                    }
                }

            }

            return color;
        }


    }
}
